import {
  myRequesy
} from '../../request/api'
import paltter from '../../platter/carc'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    DataList: [],
    params: {
      page: 1,
      page_size: 10,
      search: "",
    },
    has_next: null,
    operateList: [{
        id: 1,
        color: '#2287F9',
        icon: '/static/image/redact.png',
        title: '编辑',
        url: '/pages/launch/launch'
      },
      {
        id: 2,
        color: '#1FC064',
        icon: '/static/image/share.png',
        title: '分享',
      },
      {
        id: 3,
        color: '#3C82D9',
        icon: '/static/image/details.png',
        title: '收集详情',
        url: '/pages/collect/collect'
      },
      {
        id: 4,
        color: '#303030',
        icon: '/static/image/examine.png',
        title: '查看表单',
        url: '/pages/form/form'
      },
    ], //操作框数据
    isShow: false, //是否显示操作框
    isAdmin: true, //控制操作框高度
    isPoster: false, //是否显示海报
    shareShow: false, //分享
    activeIndex: 0 //当前索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDataList()
  },

  //获取数据列表
  getDataList() {
    wx.showLoading({
      title: '加载中',
    })
    let {
      DataList
    } = this.data
    myRequesy({
      url: 'admin/activity-list',
      data: this.data.params
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          DataList: [...DataList, ...res.data],
          has_next: res.has_next
        })
      }
      wx.hideLoading()
    })
  },


  // 搜索
  handleSearch(val) {
    this.setData({
      [`params.search`]: val.detail,
      DataList: []
    })
    this.getDataList()
  },



  // 查看表单
  handleLookForm(e) {
    wx.navigateTo({
      url: '../form/form?id=' + e.detail + '&is_admin=' + 1,
    })
  },


  //关闭操作框 函数
  headleClose() {
    this.setData({
      isShow: false
    })
  },

  //打开操作框 函数
  handleToolbar(e) {
    let {
      index,
      id
    } = e.detail
    this.setData({
      isShow: true,
      activeId: id,
      activeIndex: index
    })
  },


  // 操作框 函数
  headleSkip({
    detail: {
      index,
      url
    }
  }) {
    let {
      activeId
    } = this.data
    if (index === 1) {
      this.setData({
        shareShow: true,
        isShow: false
      })
      return
    }
    wx.navigateTo({
      url: url + '?id=' + activeId
    })

  },

  // 关闭分享弹窗
  headleCloseShare() {
    this.setData({
      shareShow: false
    })
  },


  /* 海报区域*********************************************************************************************************** */

  // 海报
  headlePoster() {
    let that = this
    let {
      DataList,
      activeIndex,
      activeId
    } = that.data
    let car = new paltter().palette()
    let userInfo = wx.getStorageSync('userInfo')
    car.views[1].url = userInfo.avatarUrl
    car.views[2].text = userInfo.nickName
    car.views[3].url = DataList[activeIndex].banner
    car.views[4].text = DataList[activeIndex].title
    myRequesy({
      url: 'get-qrcode',
      data: {
        a_id: activeId
      }
    }).then(res => {
      if (res.code === 0) {
        car.views[5].url = res.data.qrcode
        that.setData({
          template: car,
          isPoster: true,
          shareShow: false
        })
      }
    })
  },

  // 保存
  saveImg() {
    this.saveImage()
  },

  // 海报
  onImgOK(e) {
    var that = this
    var imagePath = e.detail.path;
    that.setData({
      imagePath: imagePath
    })
  },

  // 保存海报
  saveImage() {
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: that.data.imagePath,
      success: (res) => {
        that.setData({
          poster_hidden: true,
          isPoster: false
        })
        wx.showToast({
          title: '已保存到相册',
        })
        // setTimeout(() => {
        //   wx.navigateBack()
        // }, 1000)
      },
      fail() {
        wx.showModal({
          title: '保存失败！',
          content: '请检查相册访问是否授权',
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                success(res) {}
              })
            } else if (res.cancel) {}
          }
        })
      }
    });
  },

  //关闭遮罩层
  headleMask() {
    this.setData({
      isPoster: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      isShow: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      has_next
    } = this.data
    if (has_next) {
      this.setData({
        [`params.page`]: this.data.params.page + 1
      })
      this.getDataList()
    } else {
      wx.showToast({
        title: '你够了！',
        icon: "none"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let {
      activeId,
      activeIndex,
      DataList
    } = this.data
    if (res.from === 'button') {
      return {
        title: DataList[activeIndex].title,
        imageUrl: DataList[activeIndex].banner,
        path: "/pages/form/form?id=" + activeId
      }
    }
  }
})